package cn.vcjf.washcar.api.order.controller;


import cn.vcjf.washcar.api.order.entity.WashCarOrder;
import cn.vcjf.washcar.api.order.service.WashCarOrderService;
import cn.vcjf.washcar.api.order.service.impl.WxpayCallbackServiceImpl;
import cn.vcjf.washcar.api.user.service.UserService;
import cn.vcjf.washcar.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * <p>
 * 洗车订单表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/order/washCarOrder")
public class WashCarOrderController {
	@Resource
	private WashCarOrderService washCarOrderService;
	@Resource
	private UserService userService;
	@Resource
	private WxpayCallbackServiceImpl wxpayCallbackService;

	/**
	 *
	 */
	@RequestMapping("/cleanOpenId")
	public void cleanOpenId(){
		wxpayCallbackService.cleanOpenId();
	}
	/**
	 * 微信下单
	 * @param request
	 * @param washCarOrder
	 * @return
	 */
	@RequestMapping("/addWashCarOrder")
	@ResponseBody
	public TableResult addWashCarOrder(HttpServletRequest request, WashCarOrder washCarOrder) throws ParseException {

		return washCarOrderService.addWashCarOrder(request,washCarOrder);
	}

	/**
	 * 微信支付回调
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/washCarOrderCallBack")
	@ResponseBody
	public void washCarOrderCallBack(HttpServletRequest request, HttpServletResponse response) throws IOException {

		wxpayCallbackService.doPost(request,response);
	}

	@RequestMapping("/updateWashCarOrder")
	@ResponseBody
	public TableResult updateWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preUpdate();
		boolean b = washCarOrderService.updateById(washCarOrder);
		return TableResult.Result(b);
	}
	@RequestMapping("/cancelWashCarOrder")
	@ResponseBody
	public TableResult cancelWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preUpdate();
		washCarOrder.setStatus(WashCarOrder.ORDER_CANCEL);
		boolean b = washCarOrderService.updateById(washCarOrder);
		return TableResult.Result(b);
	}

	@RequestMapping("/findWashCarOrderOne")
	@ResponseBody
	public TableResult findWashCarOrderOne(WashCarOrder washCarOrder) {

		EntityWrapper<WashCarOrder> wrapper = new EntityWrapper<WashCarOrder>();

		if (washCarOrder.getId() != null) {
			wrapper.eq("o.id", washCarOrder.getId());
		}

		if (washCarOrder.getOrderNo() != null) {
			wrapper.eq("o.orderNo", washCarOrder.getOrderNo());
		}
		//Todo
		WashCarOrder result = washCarOrderService.selectWashCarOrderOne(wrapper);

		return TableResult.Result(result);
	}
	@RequestMapping("/orderDetail")
	public ModelAndView orderDetail(WashCarOrder washCarOrder, ModelAndView model) {

		EntityWrapper<WashCarOrder> wrapper = new EntityWrapper<WashCarOrder>();

		if (washCarOrder.getId() != null) {
			wrapper.eq("id", washCarOrder.getId());
		}
		if (washCarOrder.getUserId() != null) {
//			return TableResult.ErrorResult("userId不能为空");
			wrapper.eq("userId", washCarOrder.getUserId());
		}
		if (washCarOrder.getOrderNo() != null) {
			wrapper.eq("orderNo", washCarOrder.getOrderNo());
		}
		//Todo
		WashCarOrder result = washCarOrderService.selectOne(wrapper);
		model.addObject("washCarOrder",result);
		model.setViewName("orderDetail");
		return model;
	}

	@RequestMapping("/findWashCarOrderList")
	@ResponseBody
	public TableResult findWashCarOrderList(WashCarOrder washCarOrder) {

		EntityWrapper<WashCarOrder> wrapper = new EntityWrapper<WashCarOrder>();

		if (washCarOrder.getId() != null) {
			wrapper.eq("id", washCarOrder.getId());
		}
		//订单统计1下单中,2下单成功,3下单失败,4支付成功,5支付失败,6已撤销,
		//服务状态(1未服务,2已服务)
		/**
		 * status=4,serviceStatus=1 待服务
		 * status=4,serviceStatus=2 服务中
		 * status=4,serviceStatus=3 已完成
		 * status=6 已撤销
		 */
		if (washCarOrder.getUserId() == null) {
			return TableResult.ErrorResult("userId不能为空");
		}
		wrapper.eq("userId", washCarOrder.getUserId());
		if (washCarOrder.getStatus() != null) {
			wrapper.eq("status", washCarOrder.getStatus());
		}
		if (washCarOrder.getServiceStatus() != null) {
			wrapper.eq("serviceStatus", washCarOrder.getServiceStatus());
		}
		wrapper.orderBy("createTime",false);
		wrapper.orderBy("payTime",false);

		Page<WashCarOrder> result = new Page<WashCarOrder>(washCarOrder.getPage(), washCarOrder.getLimit());

		result = washCarOrderService.selectPage(result, wrapper);

		return TableResult.Result(result);

	}

	@RequestMapping("/deleteWashCarOrder")
	@ResponseBody
	public TableResult deleteWashCarOrder(WashCarOrder washCarOrder) {
		washCarOrder.preUpdate();
		boolean b = washCarOrderService.deleteById(washCarOrder.getId());
		return TableResult.Result(b);
	}

}
